﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

using Hangjing.Common;
using Hangjing.SQLServerDAL;
using Hangjing.Model;
using Hangjing.DBUtility;


public partial class Admin_shop_devicespan : AdminPageBase
{
    private string SqlWhere
    {
        get
        {
            object o = ViewState["SqlWhere"];
            return (o == null) ? "" : Convert.ToString(o);
        }
        set
        {
            ViewState["SqlWhere"] = value;
        }
    }

    protected string ordersort
    {
        get
        {
            object o = ViewState["ordersort"];
            return (o == null) ? "unid" : Convert.ToString(o);
        }
        set
        {
            ViewState["ordersort"] = value;
        }
    }

    StorageLog bll = new StorageLog();

    private string devSqlWhere
    {
        get
        {
            object o = ViewState["devSqlWhere"];
            return (o == null) ? string.Empty : o.ToString();
        }
        set
        {
            ViewState["devSqlWhere"] = value;
        }
    }

    Devices devdal = new Devices();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            tbStartTime.Text = DateTime.Now.ToShortDateString();
            this.tbEndTime.Text = DateTime.Now.ToShortDateString();
            WebUtility.SetDDLCity(DDLArea);
            initwhere();
            BindData(ordersort);
            bindDev();
        }
    }


    /// <summary>
    /// 绑定指定的数据
    /// </summary>
    protected void BindData(string oo)
    {
        this.AspNetPager1.RecordCount = bll.GeSpantCount(SqlWhere);
        this.rtpOrderlist.DataSource = bll.GetSpanList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, SqlWhere, "sid", 1);
        this.rtpOrderlist.DataBind();


        //保存此状态
        WebUtility.FixdelCookie("SearchOrderSqlWhere");

        AlertScript.AjaxRegisterScript("init();", UpdatePanel1);
        AlertScript.RegScript(this.Page, UpdatePanel1, " $('#loading-mask').hide();");
    }

    protected void bindDev()
    {
        this.AspNetPager1.RecordCount = devdal.GetCount(devSqlWhere);
        this.rtpGifts.DataSource = devdal.GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, devSqlWhere, "did", 1);
        this.rtpGifts.DataBind();
    }

    protected void btSearch_Click(object sender, EventArgs e)
    {
        initwhere();
        BindData(ordersort);
    }

    protected void initwhere()
    {

        devSqlWhere = " stock > 0";
        devSqlWhere = new RightsTool().getDevicesWhere(devSqlWhere);


        SqlWhere = "1=1 and StorageLog.stype = 1 ";
        if (tborderid.Text != "")
        {
            SqlWhere += " and id like '%" + WebUtility.InputText(tborderid.Text) + "%'";
        }


        if (tbStartTime.Text != "")
        {
            SqlWhere = SqlWhere + " and  addtime > '" + WebUtility.InputText(this.tbStartTime.Text) + "' ";
        }
        if (tbEndTime.Text != "")
        {
            SqlWhere = SqlWhere + " and  addtime <  '" + WebUtility.InputText(this.tbEndTime.Text) + " 23:59:59'";
        }
        if (DDLArea.SelectedValue != "0")
        {
            SqlWhere += " and cityid =" + DDLArea.SelectedValue + "";

            devSqlWhere += " and  cityid ='" + DDLArea.SelectedValue + "'";

            

        }
        if (tbgroup.Text != "")
        {
            SqlWhere = SqlWhere + " and EXISTS (SELECT id FROM dbo.DeliverGroup WHERE id = dbo.StorageOrder.gid AND classname LIKE '%" + WebUtility.InputText(tbgroup.Text) + "%') ";
        }

        if (tbOrderAddress.Text.Trim() != "")
        {
            SqlWhere += " and adminname like '%" + WebUtility.InputText(tbOrderAddress.Text) + "%'";
        }
        
    }


    /// <summary>
    /// 翻页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {

        BindData(ordersort);
    }
    /// <summary>
    /// 翻页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dev_PageChanged(object sender, EventArgs e)
    {

        bindDev();
    }

    

}
